package com.example.shareapp.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.shareapp.R;
import com.example.shareapp.listeners.OnclickListeners;
import com.example.shareapp.ui.activity.MainActivity;


/**
 * 自定义dialog
 */
public class DialogUtils {
    static DialogUtils dialogUtils;
    Dialog dialog;

    public static DialogUtils getInstance() {
        if (dialogUtils == null) {
            dialogUtils = new DialogUtils();
        }
        return dialogUtils;
    }






    /**
     * 用系统默认布局显示弹窗，好丑
     *
     * @param context
     * @param message
     */
    public static void showQQDialog(Context context, String message,String title) {
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);

        builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(true);
        android.support.v7.app.AlertDialog dialog = builder.create();
        dialog.show();
    }


    public static AlertDialog mDialog;
    public static View view;
    public static void showDialog(Context context,int mWidth,final OnclickListeners mOnclickListeners){
        try {
            if (mDialog != null && mDialog.isShowing()){
                mDialog.dismiss();
                mDialog = null;
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        view = LayoutInflater.from(context).inflate(R.layout.dialog_view,null,false);
        mDialog = new AlertDialog.Builder(context).setView(view).create();
        Button btn_jrrj = view.findViewById(R.id.btn_jrrj);
        Button btn_fx = view.findViewById(R.id.btn_fx);
        TextView tv_num = view.findViewById(R.id.tv_num);
        tv_num.setText(""+MainActivity.INDEX);
        btn_jrrj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                mOnclickListeners.jinriruanjian();
//                mDialog.dismiss();
            }
        });

        btn_fx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                mOnclickListeners.fenxiang();
//                dialog.dismiss();
            }
        });

        mDialog.show();
        //此处设置位置窗体大小，我这里设置为了手机屏幕宽度的3/4  注意一定要在show方法调用后再写设置窗口大小的代码，否则不起效果会
        mDialog.getWindow().setLayout(mWidth/6*5,LinearLayout.LayoutParams.WRAP_CONTENT);
    }


    public static AlertDialog getDialog(){
        return mDialog;
    }

    public static void dismissDialog(){
        try {
            if (mDialog != null && mDialog.isShowing()){
                mDialog.dismiss();
                mDialog = null;
            }
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}




